<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!--        <fieldset class="table-search-fieldset">-->
        <!--            <legend>搜索信息</legend>-->
        <!--            <div style="margin: 10px 10px 10px 10px">-->
        <!--                <form class="layui-form layui-form-pane" action="">-->
        <!--                    <div class="layui-form-item">-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户姓名</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="username" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户性别</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="sex" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户城市</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="city" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户职业</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="classify" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>-->
        <!--                        </div>-->
        <!--                    </div>-->
        <!--                </form>-->
        <!--            </div>-->
        <!--        </fieldset>-->

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"> 删除 </button>
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="refresh">刷新</button>
                <button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="startUsing">启用</button>
            </div>
        </script>

        <table class="layui-hide" id="kafka_list" lay-filter="currentTableFilter"></table>

        <!--        <script type="text/html" id="currentTableBar">-->
        <!--            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>-->
        <!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
        <!--        </script>-->

    </div>
</div>
<script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/common.js" charset="utf-8"></script>
<script>
    var form,table;
    layui.use(['form', 'table'], function () {
            form = layui.form,
            table = layui.table;

        table.render({
            id:"kafka_list",
            elem: '#kafka_list',
            url: window.reqPath("kafka/list"),
            toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print', {
            //     title: '提示',
            //     layEvent: 'LAYTABLE_TIPS',
            //     icon: 'layui-icon-tips'
            // }],
            cols: [[
                {type: "radio", width: 50},
                {field: 'id', width: 80, title: 'ID', hide: true},
                {field: 'kafka_url',title: 'kafka地址'},
                {field: 'type', title: '使用方式',templet:function (row) {
                        if(row.type =="get") return"<p style=\"color:black\">收取</p>";
                        if(row.type =="set") return"<p style=\"color:black\">发送</p>";
                        return "<p style=\"color:red\">未知</p>";
                    }},
                {field: 'status', width: 80, title: '启用',templet:function (row) {
                        if(row.status =="true") return"<p style=\"color:green\">是</p>";
                        return "<p style=\"color:red\">否</p>";
                    }}
            ]],
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('kafka_list', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus('kafka_list');
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加kafka',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../pages/kafka/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                deleteData(checkStatus);
            }else if (obj.event === 'refresh') {  // 监听删除操作
                refresh();
            }else if (obj.event === 'startUsing') {  // 监听删除操作
                layer.confirm('是否启用？', {
                    btn: ['启用','停用'] //按钮
                }, function(){
                    startUsing(checkStatus,true);
                }, function(){
                    startUsing(checkStatus,false);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
    });
    var deleteData = function (checkData) {
        var data = {};
        if(checkData.data.length>0){
            var ids = "";
            for (var i = 0; i < checkData.data.length; i++) {
                ids += checkData.data[i].id;
                if (!(i == checkData.data.length - 1)) {
                    ids += ",";
                }
            }
            data.ids = ids;
        }else{
            layer.msg("请至少选择一条数据进行删除！", {icon: 0});
            return;
        }
        $.ajax({
            type:'POST',	//请求的类型,GET、POST等
            url:window.reqPath("kafka/delete"),	//向服务器请求的地址。
            dataType:'JSON',	//预期服务器响应类型
            data:JSON.stringify(data),
            contentType:'application/json',	//向服务器发送内容的类型，默认值是：application/x-www-form-urlencoded
            cache: false,
            success:function(result,status,XMLHttpRequest){		//请求成功是执行的函数,result：服务器返回的数据，    status：服务器返回的状态，
                // layer.msg(result.msg);
                layer.msg(result.msg);
                refresh();
            },
            error:function(xhr,status,error){	//请求失败是执行的函数
            },
            complete:function(xhr,status){
                //不管请求失败还是请求成功，都执行的函数
            }
        })
    }
    var startUsing =function (checkData,return_status) {
        var data = {};
        if(checkData.data.length>0){
            var ids = "";
            for (var i = 0; i < checkData.data.length; i++) {
                ids += checkData.data[i].id;
                if (!(i == checkData.data.length - 1)) {
                    ids += ",";
                }
            }
            data.ids = ids;
        }else{
            layer.msg("请至少选择一条数据进行改动！", {icon: 0});
            return;
        }
        data.status = return_status;
        $.ajax({
            type:'POST',	//请求的类型,GET、POST等
            url:window.reqPath("kafka/startUsing"),	//向服务器请求的地址。
            dataType:'JSON',	//预期服务器响应类型
            data:JSON.stringify(data),
            contentType:'application/json',	//向服务器发送内容的类型，默认值是：application/x-www-form-urlencoded
            cache: false,
            success:function(result,status,XMLHttpRequest){		//请求成功是执行的函数,result：服务器返回的数据，    status：服务器返回的状态，
                // layer.msg(result.msg);
                layer.msg(result.msg);
                refresh();
            },
            error:function(xhr,status,error){	//请求失败是执行的函数
            },
            complete:function(xhr,status){
                //不管请求失败还是请求成功，都执行的函数
            }
        })
    }
    var refresh = function () {
        //执行搜索重载
        table.reload('kafka_list', {
            page: {
                curr: 1
            }
            , where: {
                params: ""
            }
        }, 'data');
    }
</script>

</body>
</html>